<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>Philox Counter-based RNG &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../../about.html" >
    <link rel="index" title="Index" href="../../../genindex.html" >
    <link rel="search" title="Search" href="../../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../../index.html" >
    <link rel="up" title="Bit Generators" href="index.html" >
    <link rel="next" title="numpy.random.Philox.state" href="generated/numpy.random.Philox.state.html" >
    <link rel="prev" title="numpy.random.PCG64.ctypes" href="generated/numpy.random.PCG64.ctypes.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../../index.html">
      <img border=0 alt="NumPy" src="../../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../../routines.html" >Routines</a></li>
          <li class="active"><a href="../index.html" >Random sampling (<code class="xref py py-mod docutils literal notranslate"><span class="pre">numpy.random</span></code>)</a></li>
          <li class="active"><a href="index.html" accesskey="U">Bit Generators</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="generated/numpy.random.Philox.state.html" title="numpy.random.Philox.state"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="generated/numpy.random.PCG64.ctypes.html" title="numpy.random.PCG64.ctypes"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Philox Counter-based RNG</a><ul>
<li><a class="reference internal" href="#state">State</a></li>
<li><a class="reference internal" href="#parallel-generation">Parallel generation</a></li>
<li><a class="reference internal" href="#extending">Extending</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="generated/numpy.random.PCG64.ctypes.html"
                        title="previous chapter">numpy.random.PCG64.ctypes</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="generated/numpy.random.Philox.state.html"
                        title="next chapter">numpy.random.Philox.state</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="philox-counter-based-rng">
<h1>Philox Counter-based RNG<a class="headerlink" href="#philox-counter-based-rng" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="numpy.random.Philox">
<em class="property">class </em><code class="sig-prename descclassname">numpy.random.</code><code class="sig-name descname">Philox</code><span class="sig-paren">(</span><em class="sig-param">seed=None</em>, <em class="sig-param">counter=None</em>, <em class="sig-param">key=None</em><span class="sig-paren">)</span><a class="headerlink" href="#numpy.random.Philox" title="Permalink to this definition">¶</a></dt>
<dd><p>Container for the Philox (4x64) pseudo-random number generator.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>seed</strong><span class="classifier">{None, int, array_like[ints], SeedSequence}, optional</span></dt><dd><p>A seed to initialize the <a class="reference internal" href="generated/numpy.random.BitGenerator.html#numpy.random.BitGenerator" title="numpy.random.BitGenerator"><code class="xref py py-obj docutils literal notranslate"><span class="pre">BitGenerator</span></code></a>. If None, then fresh,
unpredictable entropy will be pulled from the OS. If an <code class="docutils literal notranslate"><span class="pre">int</span></code> or
<code class="docutils literal notranslate"><span class="pre">array_like[ints]</span></code> is passed, then it will be passed to
<a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a> to derive the initial <a class="reference internal" href="generated/numpy.random.BitGenerator.html#numpy.random.BitGenerator" title="numpy.random.BitGenerator"><code class="xref py py-obj docutils literal notranslate"><span class="pre">BitGenerator</span></code></a> state. One may also
pass in a <a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a> instance.</p>
</dd>
<dt><strong>counter</strong><span class="classifier">{None, int, array_like}, optional</span></dt><dd><p>Counter to use in the Philox state. Can be either
a Python int (long in 2.x) in [0, 2**256) or a 4-element uint64 array.
If not provided, the RNG is initialized at 0.</p>
</dd>
<dt><strong>key</strong><span class="classifier">{None, int, array_like}, optional</span></dt><dd><p>Key to use in the Philox state.  Unlike <code class="docutils literal notranslate"><span class="pre">seed</span></code>, the value in key is
directly set. Can be either a Python int in [0, 2**128) or a 2-element
uint64 array. <em class="xref py py-obj">key</em> and <code class="docutils literal notranslate"><span class="pre">seed</span></code> cannot both be used.</p>
</dd>
</dl>
</dd>
</dl>
<p class="rubric">Notes</p>
<p>Philox is a 64-bit PRNG that uses a counter-based design based on weaker
(and faster) versions of cryptographic functions <a class="reference internal" href="#r40f8e2ad755a-1" id="id1">[1]</a>. Instances using
different values of the key produce independent sequences.  Philox has a
period of <img class="math" src="../../../_images/math/97309473714c9e4388c1f30bdb811ba9d2ad6731.svg" alt="2^{256} - 1"/> and supports arbitrary advancing and jumping
the sequence in increments of <img class="math" src="../../../_images/math/91018ad7005c40f683c645b99dd28ae741d2de00.svg" alt="2^{128}"/>. These features allow
multiple non-overlapping sequences to be generated.</p>
<p><code class="docutils literal notranslate"><span class="pre">Philox</span></code> provides a capsule containing function pointers that produce
doubles, and unsigned 32 and 64- bit integers. These are not
directly consumable in Python and must be consumed by a <code class="docutils literal notranslate"><span class="pre">Generator</span></code>
or similar object that supports low-level access.</p>
<p><strong>State and Seeding</strong></p>
<p>The <code class="docutils literal notranslate"><span class="pre">Philox</span></code> state vector consists of a 256-bit value encoded as
a 4-element uint64 array and a 128-bit value encoded as a 2-element uint64
array. The former is a counter which is incremented by 1 for every 4 64-bit
randoms produced. The second is a key which determined the sequence
produced. Using different keys produces independent sequences.</p>
<p>The input <code class="docutils literal notranslate"><span class="pre">seed</span></code> is processed by <a class="reference internal" href="generated/numpy.random.SeedSequence.html#numpy.random.SeedSequence" title="numpy.random.SeedSequence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence</span></code></a> to generate the key. The
counter is set to 0.</p>
<p>Alternately, one can omit the <code class="docutils literal notranslate"><span class="pre">seed</span></code> parameter and set the <code class="docutils literal notranslate"><span class="pre">key</span></code> and
<code class="docutils literal notranslate"><span class="pre">counter</span></code> directly.</p>
<p><strong>Parallel Features</strong></p>
<p>The preferred way to use a BitGenerator in parallel applications is to use
the <a class="reference internal" href="generated/numpy.random.SeedSequence.spawn.html#numpy.random.SeedSequence.spawn" title="numpy.random.SeedSequence.spawn"><code class="xref py py-obj docutils literal notranslate"><span class="pre">SeedSequence.spawn</span></code></a> method to obtain entropy values, and to use these
to generate new BitGenerators:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy.random</span> <span class="kn">import</span> <span class="n">Generator</span><span class="p">,</span> <span class="n">Philox</span><span class="p">,</span> <span class="n">SeedSequence</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sg</span> <span class="o">=</span> <span class="n">SeedSequence</span><span class="p">(</span><span class="mi">1234</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rg</span> <span class="o">=</span> <span class="p">[</span><span class="n">Generator</span><span class="p">(</span><span class="n">Philox</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">sg</span><span class="o">.</span><span class="n">spawn</span><span class="p">(</span><span class="mi">10</span><span class="p">)]</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">Philox</span></code> can be used in parallel applications by calling the <code class="docutils literal notranslate"><span class="pre">jumped</span></code>
method  to advances the state as-if <img class="math" src="../../../_images/math/91018ad7005c40f683c645b99dd28ae741d2de00.svg" alt="2^{128}"/> random numbers have
been generated. Alternatively, <code class="docutils literal notranslate"><span class="pre">advance</span></code> can be used to advance the
counter for any positive step in [0, 2**256). When using <code class="docutils literal notranslate"><span class="pre">jumped</span></code>, all
generators should be chained to ensure that the segments come from the same
sequence.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy.random</span> <span class="kn">import</span> <span class="n">Generator</span><span class="p">,</span> <span class="n">Philox</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">bit_generator</span> <span class="o">=</span> <span class="n">Philox</span><span class="p">(</span><span class="mi">1234</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rg</span> <span class="o">=</span> <span class="p">[]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">):</span>
<span class="gp">... </span>   <span class="n">rg</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Generator</span><span class="p">(</span><span class="n">bit_generator</span><span class="p">))</span>
<span class="gp">... </span>   <span class="n">bit_generator</span> <span class="o">=</span> <span class="n">bit_generator</span><span class="o">.</span><span class="n">jumped</span><span class="p">()</span>
</pre></div>
</div>
<p>Alternatively, <code class="docutils literal notranslate"><span class="pre">Philox</span></code> can be used in parallel applications by using
a sequence of distinct keys where each instance uses different key.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">key</span> <span class="o">=</span> <span class="mi">2</span><span class="o">**</span><span class="mi">96</span> <span class="o">+</span> <span class="mi">2</span><span class="o">**</span><span class="mi">33</span> <span class="o">+</span> <span class="mi">2</span><span class="o">**</span><span class="mi">17</span> <span class="o">+</span> <span class="mi">2</span><span class="o">**</span><span class="mi">9</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rg</span> <span class="o">=</span> <span class="p">[</span><span class="n">Generator</span><span class="p">(</span><span class="n">Philox</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="o">+</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)]</span>
</pre></div>
</div>
<p><strong>Compatibility Guarantee</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Philox</span></code> makes a guarantee that a fixed <code class="docutils literal notranslate"><span class="pre">seed</span></code> will always produce
the same random integer stream.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r40f8e2ad755a-1"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p>John K. Salmon, Mark A. Moraes, Ron O. Dror, and David E. Shaw,
“Parallel Random Numbers: As Easy as 1, 2, 3,” Proceedings of
the International Conference for High Performance Computing,
Networking, Storage and Analysis (SC11), New York, NY: ACM, 2011.</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">numpy.random</span> <span class="kn">import</span> <span class="n">Generator</span><span class="p">,</span> <span class="n">Philox</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rg</span> <span class="o">=</span> <span class="n">Generator</span><span class="p">(</span><span class="n">Philox</span><span class="p">(</span><span class="mi">1234</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">rg</span><span class="o">.</span><span class="n">standard_normal</span><span class="p">()</span>
<span class="go">0.123  # random</span>
</pre></div>
</div>
<dl class="field-list simple">
<dt class="field-odd">Attributes</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>lock: threading.Lock</strong></dt><dd><p>Lock instance that is shared so that the same bit git generator can
be used in multiple Generators without corrupting the state. Code that
generates values from a bit generator should hold the bit generator’s
lock.</p>
</dd>
</dl>
</dd>
</dl>
</dd></dl>

<div class="section" id="state">
<h2>State<a class="headerlink" href="#state" title="Permalink to this headline">¶</a></h2>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.random.Philox.state.html#numpy.random.Philox.state" title="numpy.random.Philox.state"><code class="xref py py-obj docutils literal notranslate"><span class="pre">state</span></code></a></p></td>
<td><p>Get or set the PRNG state</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="parallel-generation">
<h2>Parallel generation<a class="headerlink" href="#parallel-generation" title="Permalink to this headline">¶</a></h2>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.random.Philox.advance.html#numpy.random.Philox.advance" title="numpy.random.Philox.advance"><code class="xref py py-obj docutils literal notranslate"><span class="pre">advance</span></code></a>(delta)</p></td>
<td><p>Advance the underlying RNG as-if delta draws have occurred.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.random.Philox.jumped.html#numpy.random.Philox.jumped" title="numpy.random.Philox.jumped"><code class="xref py py-obj docutils literal notranslate"><span class="pre">jumped</span></code></a>([jumps])</p></td>
<td><p>Returns a new bit generator with the state jumped</p></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="extending">
<h2>Extending<a class="headerlink" href="#extending" title="Permalink to this headline">¶</a></h2>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="generated/numpy.random.Philox.cffi.html#numpy.random.Philox.cffi" title="numpy.random.Philox.cffi"><code class="xref py py-obj docutils literal notranslate"><span class="pre">cffi</span></code></a></p></td>
<td><p>CFFI interface</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="generated/numpy.random.Philox.ctypes.html#numpy.random.Philox.ctypes" title="numpy.random.Philox.ctypes"><code class="xref py py-obj docutils literal notranslate"><span class="pre">ctypes</span></code></a></p></td>
<td><p>ctypes interface</p></td>
</tr>
</tbody>
</table>
</div>
</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>